package cn.iocoder.yudao.module.system.controller.admin.erp.operatelog;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.erp.operatelog.vo.ErpOperateLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.operatelog.vo.ErpOperateLogRespVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.operatelog.vo.ErpOperateLogSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.operatelog.ErpOperateLogDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.erp.operatelog.ErpOperateLogService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - erp操作日志")
@RestController
@RequestMapping("/erp/operate-log")
@Validated
public class ErpOperateLogController {

    @Resource
    private ErpOperateLogService erpOperateLogService;
    @Resource
    private AdminUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建erp操作日志")
    @PreAuthorize("@ss.hasPermission('erp:operate-log:create')")
    public CommonResult<Long> createOperateLog(@Valid @RequestBody ErpOperateLogSaveReqVO createReqVO) {
        return success(erpOperateLogService.createOperateLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新erp操作日志")
    @PreAuthorize("@ss.hasPermission('erp:operate-log:update')")
    public CommonResult<Boolean> updateOperateLog(@Valid @RequestBody ErpOperateLogSaveReqVO updateReqVO) {
        erpOperateLogService.updateOperateLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除erp操作日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:operate-log:delete')")
    public CommonResult<Boolean> deleteOperateLog(@RequestParam("id") Long id) {
        erpOperateLogService.deleteOperateLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得erp操作日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:operate-log:query')")
    public CommonResult<ErpOperateLogRespVO> getOperateLog(@RequestParam("id") Long id) {
        ErpOperateLogRespVO operateLog = BeanUtils.toBean(erpOperateLogService.getOperateLog(id), ErpOperateLogRespVO.class);
        if (ObjectUtil.isNotNull(operateLog)){
            operateLog.setOperate(userService.getUser(operateLog.getUserId()).getNickname());
        }
        return success(operateLog);
    }

    @GetMapping("/page")
    @Operation(summary = "获得erp操作日志分页")
//    @PreAuthorize("@ss.hasPermission('erp:operate-log:query')")
    public CommonResult<PageResult<ErpOperateLogRespVO>> getOperateLogPage(@Valid ErpOperateLogPageReqVO pageReqVO) {
        Long loginUserId= SecurityFrameworkUtils.getLoginUserId();
        AdminUserDO user=userService.getUser(loginUserId);
        PageResult<ErpOperateLogRespVO> pageResult = new PageResult<>();
        if(ObjectUtil.isNotNull(user)&&(ObjectUtil.equal(user.getId(),1L)||ObjectUtil.equal(user.getRoleCode(),user.getRoleCode()))){
        pageResult=BeanUtils.toBean(erpOperateLogService.getOperateLogPage(pageReqVO), ErpOperateLogRespVO.class);
        pageResult.getList().forEach(e->{e.setOperate(userService.getUser(e.getUserId()).getNickname());});}
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出erp操作日志 Excel")
    @PreAuthorize("@ss.hasPermission('erp:operate-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOperateLogExcel(@Valid ErpOperateLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpOperateLogDO> list = erpOperateLogService.getOperateLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "erp操作日志.xls", "数据", ErpOperateLogRespVO.class,
                        BeanUtils.toBean(list, ErpOperateLogRespVO.class));
    }

}